package com.woniu.car.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.bean.R;
import com.woniu.car.bean.*;
import com.woniu.car.service.*;
import com.woniu.need.service.ITTransportService;
import com.woniu.order.bean.TGoods;
import com.woniu.order.bean.TLogistics;
import com.woniu.order.bean.TOrder;
import com.woniu.order.service.ITGoodsService;
import com.woniu.order.service.ITLogisticsService;
import com.woniu.order.service.ITOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kk
 * @since 2022-08-12
 */
@RestController
@RequestMapping("/receipt")
@CrossOrigin
public class TReceiptController {
    @Autowired
    private ITReceiptService receiptService;
    @Autowired
    private ITOrderService orderServicel;

    @Autowired
    private ITGoodsService goodsService;

    @Autowired
    private ITLogisticsService logisticsService;

    @Autowired
    private ITCarService carService;

    @GetMapping
    public R selectAll(@RequestParam Integer currentPage, Integer pageSize, Integer orderId, String orderName){
        R r =null;
        IPage<TReceipt> iPage = new Page<>(currentPage,pageSize);
        QueryWrapper<TReceipt> qw = new QueryWrapper<>();
        if(StringUtils.hasText(orderName)){
            qw.like("username",orderName);
        }
        if(orderId != null){
            qw.like("order_id",orderId);
        }
        IPage<TReceipt> page = receiptService.page(iPage, qw);
        List<TReceipt> records = page.getRecords();
        records.forEach(e->{
            TOrder order = orderServicel.getById(e.getOrderId());
            e.setOrder(order);
        });
        long total = page.getTotal();
        Map map= new HashMap();
        map.put("records",records);
        map.put("total",total);
        r=R.OK();
        r.setData(map);
        return r;
    }
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id){
        TReceipt TReceipt = receiptService.getById(id);
        R r =null;
        r=R.OK();
        r.setData(TReceipt);
        return r;

    }

    @PutMapping
    public R update(@RequestBody TReceipt tReceipt){
        //查看是否修改回单状态
        Integer status = tReceipt.getReceiptStatus();
        System.out.println(status);
        Integer orderId = tReceipt.getOrderId();
        QueryWrapper qw = new QueryWrapper();
        qw.eq("order_id",orderId);
        TLogistics tLogistics = logisticsService.getOne(qw);
        String carPlate = tLogistics.getCarPlate();
        QueryWrapper qw2 = new QueryWrapper();
        qw2.eq("car_plate",carPlate);
        TCar car = carService.getOne(qw2);


        boolean b = receiptService.updateById(tReceipt);
        R r =null;
        if(b){
            if(status == 1){
                car.setStatus(1);
                carService.updateById(car);
                r=R.OK();
            } else {
                car.setStatus(0);
                carService.updateById(car);
            }
        }else {
            r=R.ERROR();
        }
        return r;
    }

    //传入orderid 连表查询数据
    @GetMapping("/more/{id}")
    public R moreReceipt(@PathVariable Integer id){

        QueryWrapper qw = new QueryWrapper();
        qw.eq("order_id",id);
//        List<TGoods> list = goodsService.list(qw);
//        TGoods tGoods = list.get(0);
        //对应字段货物对象
        TGoods goods = goodsService.getOne(qw);
        TOrder order = orderServicel.getById(id);
        //装入货物对象
        order.setGoods(goods);

        TLogistics logistics = logisticsService.getOne(qw);
        order.setLogistics(logistics);
        R r =R.OK();
        List list= new ArrayList();
        list.add(order);
        r.setData(list);

        System.out.println(list);
        return r;


    }

}
